#include "TratamentoSerial.h"
#include "WirelessProtocols/Console.h"
#include "Transceivers/Transceivers.h"
#include "WirelessProtocols/SymbolTime.h"
#include "Transceivers/Security.h"
#include "WirelessProtocols/MCHP_API.h"

#define true TRUE
#define bool BOOL
#define false FALSE
#define byte BYTE

BOOL possuiDado() {
    if (U2STAbits.URXDA) {
        return TRUE;
    } else {
        return FALSE;
    }
}

void mensagemRTC() {
    byte *ptrVetor;
    byte horario[7];
    byte aux1, aux2;
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite os segundos \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[0] = aux2 | aux1;
  //  while (!ConsoleIsPutReady());
 //   PrintChar(horario[0]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite os minutos \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[1] = aux2 | aux1;
 //   while (!ConsoleIsPutReady());
 //   PrintChar(horario[1]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite a hora \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[2] = aux2 | aux1;
 //   while (!ConsoleIsPutReady());
 //   PrintChar(horario[2]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o dia da semana \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[3] = aux2 | aux1;
  //  while (!ConsoleIsPutReady());
//    PrintChar(horario[3]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o dia \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[4] = aux2 | aux1;
//    while (!ConsoleIsPutReady());
 //   PrintChar(horario[4]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o mes \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[5] = aux2 | aux1;
 //   while (!ConsoleIsPutReady());
 //   PrintChar(horario[5]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o ano \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[6] = aux2 | aux1;
 //   while (!ConsoleIsPutReady());
  //  PrintChar(horario[6]);
    ptrVetor = &horario[0];
    envioMensagemAjusteRTC(ptrVetor);
}

void mensagemServicoDados() {
    int no;
    byte *ptrVetor;
    byte horario[7];
    byte aux1, aux2;
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o no que deseja receber a medida\r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 * 10;
    no = aux2 + aux1;
//    while (!ConsoleIsPutReady());
//    PrintDec(no);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite os segundos \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[0] = aux2 | aux1;
//    while (!ConsoleIsPutReady());
//    PrintChar(horario[0]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite os minutos \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    while (!ConsoleIsPutReady());
    horario[1] = aux2 | aux1;
//    while (!ConsoleIsPutReady());
//    PrintChar(horario[1]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite a hora \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[2] = aux2 | aux1;
//    while (!ConsoleIsPutReady());
//    PrintChar(horario[2]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o dia da semana \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[3] = aux2 | aux1;
//    while (!ConsoleIsPutReady());
//    PrintChar(horario[3]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o dia \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[4] = aux2 | aux1;
//    while (!ConsoleIsPutReady());
//    PrintChar(horario[4]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o mes \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[5] = aux2 | aux1;
//    while (!ConsoleIsPutReady());
//    PrintChar(horario[5]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o ano \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[6] = aux2 | aux1;
//    while (!ConsoleIsPutReady());
//    PrintChar(horario[6]);
    ptrVetor = &horario[0];
    mensagemReqDados(ptrVetor, no);
}

void mensagemReqDadosRtc() {
    byte aux1, aux2;
    int no;
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o no que deseja requisitar o horario \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 * 10;
    no = aux2 + aux1;
//    while (!ConsoleIsPutReady());
//    PrintDec(no);
    envioMensagemReqHorarioRTC(no);
}

void mensagemPing() {
    byte aux1, aux2;
    int no;
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o no que deseja pingar \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 * 10;
    no = aux2 + aux1;
//    while (!ConsoleIsPutReady());
//    PrintDec(no);
    ping(no);
}

void mensagemRTCUmNo() {
    int no;
    byte *ptrVetor;
    byte horario[7];
    byte aux1, aux2;
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o no que deseja receber ajustar o RTC\r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 * 10;
    no = aux2 + aux1;
//    while (!ConsoleIsPutReady());
//    PrintDec(no);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite os segundos \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[0] = aux2 | aux1;
//    while (!ConsoleIsPutReady());
//    PrintChar(horario[0]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite os minutos \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[1] = aux2 | aux1;
//    while (!ConsoleIsPutReady());
//    PrintChar(horario[1]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite a hora \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[2] = aux2 | aux1;
//    while (!ConsoleIsPutReady());
//    PrintChar(horario[2]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o dia da semana \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[3] = aux2 | aux1;
//    while (!ConsoleIsPutReady());
//    PrintChar(horario[3]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o dia \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[4] = aux2 | aux1;
//    while (!ConsoleIsPutReady());
//    PrintChar(horario[4]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o mes \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[5] = aux2 | aux1;
//    while (!ConsoleIsPutReady());
//    PrintChar(horario[5]);
    while (!ConsoleIsPutReady());
    Printf("\r\nDigite o ano \r\n");
    while (!ConsoleIsGetReady());
    aux1 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux1 + 0x30));
    while (!ConsoleIsGetReady());
    aux2 = ConsoleGet() - 0x30;
    while (!ConsoleIsPutReady());
    ConsolePut((char) (aux2 + 0x30));
    aux1 = aux1 << 4;
    horario[6] = aux2 | aux1;
//    while (!ConsoleIsPutReady());
//    PrintChar(horario[6]);
    ptrVetor = &horario[0];
    envioMensagemAjusteRTCUmNo(ptrVetor, no);
}

void tratamentoMensagemProtocoloSerial(byte *vetorPedidoSerial, int tamanhoMensagem) {
    if (*vetorPedidoSerial == IDMensagemRTCProtocolo) {

    } else if (*vetorPedidoSerial == IDMensagemRequisicaoDadosProtocolo) {
        vetorPedidoSerial++;
        int aux;
        int aux2;
        byte vetorHorario[7];
        byte vetorMedidas[100];
        int tempCountMedidas[1];
        tempCountMedidas[0] = 0;
        int tempCountMedidas2 = tempCountMedidas[0];
        for (aux = 0; aux < 7; aux++) {
            vetorHorario[aux] = *vetorPedidoSerial;
            vetorPedidoSerial++;
        }
        for (aux2 = 0; aux2 < (tamanhoMensagem - 8); aux2++) {
            mensagemReqDadosprotocolo(&vetorHorario[0], *vetorPedidoSerial, &vetorMedidas[tempCountMedidas2], &tempCountMedidas[0]);
            vetorPedidoSerial++;
            tempCountMedidas2 = tempCountMedidas[0];
        }
        int tempPrint;
//        while (!ConsoleIsPutReady());
//        Printf("\r\n Medidas: ");
//        while (!ConsoleIsPutReady());
//        PrintChar(StartBit232);
//        while (!ConsoleIsPutReady());
//        Printf(" ");
//        for (tempPrint = 0; tempPrint < tempCountMedidas2; tempPrint++) {
//            while (!ConsoleIsPutReady());
//            PrintChar(vetorMedidas[tempPrint]);
//            while (!ConsoleIsPutReady());
//            Printf(" ");
//        }
//        while (!ConsoleIsPutReady());
//        PrintChar(StopBit232);
//        while (!ConsoleIsPutReady());
//        Printf("\r\n");
        while (!ConsoleIsPutReady());
        ConsolePut(StartBitResposta232);
        for (tempPrint = 0; tempPrint < tempCountMedidas2; tempPrint++) {
            while (!ConsoleIsPutReady());
            ConsolePut(vetorMedidas[tempPrint]);
        }
        while (!ConsoleIsPutReady());
        ConsolePut(StopBitResposta232);
    } else if (*vetorPedidoSerial == IDMensagemRTCUmNoumNoProtocolo) {

    }
}
